package com.ebuy.shopping.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ebuy.shopping.common.utils.Contants;
import com.ebuy.shopping.model.Category;
import com.ebuy.shopping.model.News;
import com.ebuy.shopping.model.Notice;
import com.ebuy.shopping.model.Product;
import com.ebuy.shopping.model.User;
import com.ebuy.shopping.service.ICategoryService;
import com.ebuy.shopping.service.INewsService;
import com.ebuy.shopping.service.INoticeService;
import com.ebuy.shopping.service.IProductService;
import com.ebuy.shopping.service.IUserService;

@Controller
public class LoginController
{
    @Autowired
    private IUserService userService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IProductService productService;

    @Autowired
    private INoticeService noticeService;
    @Autowired
    private INewsService newsService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Model model) throws Exception
    {
        // 栏目显示
        List<Category> categoriesIndex = categoryService.getCategoriesByIndex();
        model.addAttribute("categoriesIndex", categoriesIndex);
        List<Category> categoriesSeconde = categoryService
                .getCategoriesBySecondIndex();
        model.addAttribute("categoriesSeconde", categoriesSeconde);
        return "login/login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    private String login(String userName, String password, HttpSession session,
            Model model) throws Exception
    {

        User user = userService.login(userName);

        if (user != null)
        {
            String userId = user.getUserId();
            System.out.println("登录名：" + userId);
            String psw = user.getPassword();
            System.out.println("用户密码：" + psw);
            if (userId.equals(userName) && psw.equals(password))
            {
                // 用户信息
                session.setAttribute("user", user);
                // 一级栏目信息

            }
            else
            {
                return "redirect:/login";
            }

        }
        // 栏目显示
        List<Category> categoriesIndex = categoryService.getCategoriesByIndex();
        model.addAttribute("categoriesIndex", categoriesIndex);
        List<Category> categoriesSeconde = categoryService
                .getCategoriesBySecondIndex();
        model.addAttribute("categoriesSeconde", categoriesSeconde);
        // 导航
        Map<String, List<Category>> navigationMap = new HashMap<String, List<Category>>();
        for (Category category : categoriesIndex)
        {
            int catId = category.getId();
            String catName = category.getName();
            List<Category> secondCat = categoryService
                    .getCategoriesByParentId(catId);
            if (secondCat != null)
            {
                navigationMap.put(catName, secondCat);
            }

        }
        model.addAttribute("navigationMap", navigationMap);

        // 今日特价
        List<Product> recommendProducts = productService
                .getProductsBySaleType(Contants.SALE_TYPE_RECOMMEND);
        model.addAttribute("recommendProducts", recommendProducts);
        // 热卖产品
        List<Product> hotProducts = productService
                .getProductsBySaleType(Contants.SALE_TYPE_HOT);
        model.addAttribute("hotProducts", hotProducts);
        // 最新公告
        List<Notice> notices = noticeService.getNotices();
        model.addAttribute("notices", notices);
        // 新闻动态
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("order", "create_time");
        map.put("sort", "asc");
        map.put("offset", 0);
        map.put("pageSize", 7);
        List<News> news = newsService.getNewsPage(map);
        model.addAttribute("news", news);
        if ("admin".equals(userName))
        {
            return "manage/index";
        }
        return "main/index";
    }

    @RequestMapping(value = "/manage", method = RequestMethod.GET)
    public String manage() throws Exception
    {
        return "manage/index";
    }
}
